package common

import (
	v1 "gitee.com/sweetsunnyflower/carpooling-server-go/app/http/controllers/api/v1"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/requests"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/config"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/file"
	"github.com/gofiber/fiber/v2"
)

type UploadsController struct {
	v1.BaseController
}

func (controller *UploadsController) Store(c *fiber.Ctx) error {

	userId := controller.Auth(c)

	request := new(requests.UploadRequest)

	if errors := requests.ValidateUpload(c, request); len(errors) > 0 {
		return controller.ValidationError(c, errors)
	}

	if request.Type == "image" {
		filePath, err := file.SaveUploadAvatar(c, request.Avatar, userId)
		if err != nil {
			return controller.Faield(c, "上传失败")
		}
		return controller.Json(c, fiber.Map{"avatar": config.Get("app.url") + filePath})
	}

	return controller.Faield(c, "不支持的类型")
}
